var gridObj;
var perm;
$.post('/queryperm', function(result) {
    perm = result.perm;
});　 //$.post('Server.aspx', { id: idValue }, function (text, status) { alert(text); });

$(function() {

    $(window).load(function() {
        /*提取验证权限*/
        $.ajax({
            url: '/queryperm',
            type: 'POST',
            success: function(data) {
                perm = data.perm;
                //alert(data.perm);
                if (perm == 4) {
                    $('#delete').hide();
                    $('#add').hide();
                };
                if (perm == 2) {
                    $('#add').hide();
                }
                //初始化dialog里面的项目下拉框
                if (perm == 3) {
                    $.ajax({
                        url: '/query_task_project',
                        type: 'POST',
                        success: function(data) {
                            //alert(JSON.stringify(data));
                            $.each(data, function(index, value) {
                                $('#project1').append('<option value="' + value.id + '">' + value.name + '</option>');
                            });
                            //初始化dialog里面的组员下拉框
                            var data = {
                                projectid: $('#project1').val(),
                            };
                            //alert(JSON.stringify(data.projectid));
                            $.ajax({
                                url: '/query_task_project_user',
                                type: 'POST',
                                data: data,
                                success: function(data) {
                                    $.each(data, function(index, value) {
                                        $('#executor2').append('<option value="' + value.id + '">' + value.name + '</option>');
                                    });
                                },
                                error: function(data) {
                                    alert('error');
                                },
                            });
                        },
                        error: function(data) {
                            alert('error');
                        }
                    });
                };

                },
            error: function() {
                alert('perm.error!');
            },
        });

        datetimepicker();
        $("#project1").change(function() {
            $('#executor2').empty();
            var data = {
                projectid: $('#project1').val(),
            };
            //alert(JSON.stringify(data.projectid));
            $.ajax({
                url: '/query_task_project_user',
                type: 'POST',
                data: data,
                success: function(data) {
                    $.each(data, function(index, value) {
                        $('#executor2').append('<option value="' + value.id + '">' + value.name + '</option>');
                    });
                },
                error: function(data) {},
            });
        });
    });

    //加上日期控件
    function datetimepicker() {
        var self = this;
        $('#startDate1,#endDate1,#startDate11,#endDate11').datetimepicker({
            format: 'yyyy-mm-dd',
            startView: 2,
            minView: 2,
            autoclose: true,
            language: 'zh-CN'
        });
    };
    gridObj = $.fn.bsgrid.init('reportTable', {
        url: '/querytaskManagement',
        pageSize: 10,
        pageSizeSelect: true,
        pageSizeForGrid: [5, 10, 15, 20],
        displayBlankRows: false,
        stripeRows: true,
        rowHoverColor: true,
    });
});

function checkbox(record, rowIndex, colIndex, options) {
    return '<input type="checkbox" value="' + gridObj.getColumnValue(rowIndex, gridObj.getColumnModel(colIndex).index) + '"/>';
};

function operate(record, rowIndex, colIndex, options) {
    flag = gridObj.getRecordIndexValue(record, 'progress');
    if (perm > 3)
        return '<button type="button" class="btn btn-xs btn-primary s-w50" onclick="task.modtask(' + rowIndex + ')">修改</button>';
    else if (flag == 0)
        return '<button type="button" class="btn btn-xs btn-primary s-w50" onclick="task.modtask(' + rowIndex + ')">修改</button> <button type="button" class="btn btn-xs btn-primary s-w50" onclick="task.delete(' + rowIndex + ')">  删除</button>';
    else
        return '<button type="button" class="btn btn-xs btn-primary s-w50" onclick="task.modtask(' + rowIndex + ')">修改</button>';
};
window.operate = operate;

var task = {

    /* 初始化 */
    init: function() {
        var self = this;
        self.initEvent();
        self.validate();
    },


    /* 初始化事件 */
    initEvent: function() {
        var self = this;

        /*批量删除*/
        $('#delete').click(function() {
            if (confirm('确认删除吗')) {
                var ids1 = gridObj.getCheckedValues('id');
                var ids = '';
                for (var i = 0; i < ids1.length; i++) {
                    if (i == ids1.length - 1) {
                        ids = ids + ids1[i];
                    } else {
                        ids = ids + ids1[i] + ',';
                    }
                }
                //alert(JSON.stringify(ids));
                $.ajax({
                    url: '/deletetask',
                    type: 'POST',
                    data: {
                        id: ids
                    },
                    success: function(data) {
                        block.showMsg('删除成功');
                        //alert('删除成功');
                        gridObj.refreshPage();
                    },
                    error: function(data) {
                        alert('删除失败');
                    },
                });
            };
        });

        /* 点击搜索 */
        $('#search').click(function() {
            //alert(JSON.stringify(gridObj.getAllRecords()));
            var data = {
                name1: $('#name1').val(),
                executor1: $('#executor1').val(),
                startDate1: $('#startDate1').val(),
                startDate11: $('#startDate11').val(),
                endDate1: $('#endDate1').val(),
                endDate11: $('#endDate11').val(),
                priority1: $('#priority1').val(),
                progress: $('#progress1').val(),
            };
            //alert(JSON.stringify(data.progress));
            gridObj.search(data);
        });

        /* 点击添加 */
        $('#add').click(function() {
            $('#hideshow').show();
            $('#executor1').show();
            $('#executorshowhide').show();
            self.showDialog();
        });

        $('#confirmAdd').click(function() {
            if ($('#dialog_form').valid()) {
                var data = {
                    uid1: $('#uid').val(),
                    name1: $('#name2').val(),
                    executorid: $('#executor2').val(),
                    startDate1: $('#btn11').val(),
                    endDate1: $('#btn22').val(),
                    priority1: $('#priority2').val(),
                    work: $('#work').val(),
                    description: $('#description').val(),
                    progress: $('#progress').val(),
                    project1: $('#project1').val(),
                };
                // alert(JSON.stringify(data.uid1));
                //alert(JSON.stringify(data));
                $.ajax({
                    url: '/add_task',
                    type: 'POST',
                    data: data,
                    success: function(data) {
                        gridObj.refreshPage();
                        $('#uid').val(0);
                        self.closeDialog();
                        //alert('操作成功');
                        block.showMsg('操作成功');
                    },
                    error: function(data) {
                        alert(error);
                    },
                });
            }
        });
    },
    //
    /*点击删除*/
    delete: function(rowIndex) {
        if (confirm('确认删除吗')) {
            var data = {
                id: gridObj.getRecord(rowIndex).id,
            };
            //alert(JSON.stringify(data.id));
            $.ajax({
                url: '/deletetask',
                type: 'POST',
                data: data,
                success: function(data) {
                    block.showMsg('操作成功');
                    //alert('删除成功');
                    gridObj.refreshPage();
                },
                error: function(data) {
                    alert('删除失败');
                },
            });
        };
    },

    /*点击修改*/
    modtask: function(rowIndex) {
        if ($('#uid').val() != null) {
            $('#hideshow').hide();
        };
        $('#executorshowhide').hide();
        task.showDialog();
        var data = gridObj.getRecord(rowIndex);
        //alert(JSON.stringify(data));
        $('#uid').val(data.id);
        $('#name2').val(data.taskname);
        $('#description').val(data.description);
        $('#work').val(data.work_load);
        $('#executor2').val(data.uid);
        $('#btn11').val(data.begin_date);
        $('#btn22').val(data.end_date);
        $('#priority2').val(data.priority);
        $('#progress').val(data.progress);
        //alert($('#uid').val());
    },

    /* 打开对话框 */
    showDialog: function() {
        var self = this;
        self.clearForm();
        $('#dialog').modal('show');
        $('#btn11,#btn22').datetimepicker({
            format: 'yyyy-mm-dd',
            startView: 2,
            minView: 2,
            autoclose: true,
            language: 'zh-CN'
        });
    },

    /* 关闭对话框 */
    closeDialog: function() {
        $('#dialog').modal('hide');
    },

    /* 清空对话框表单 */
    clearForm: function() {
        $('#dialog_form')[0].reset();
        $('#dialog_form .error').text('');
    },

    validate: function(){
        $.validator.addMethod('isSmaller', function(value, element) {
            var beginDate = $('#btn11').val();
            return this.optional(element) || (new Date(value) >= new Date(beginDate));
        }, '结束日期不得小于开始日期');

        $('#dialog_form').validate({
            rules: {
                name2: 'required',
                btn11: {
                    required: true,
                    date: true
                },
                btn22: {
                    isSmaller: true
                },
                project1: 'required',
                executor2: 'required',
                progress: {
                    required: true,
                    range:[0, 100]
                },
                work: {
                    required: true,
                    number: true
                },

            },
            messages: {
                name2: '请填写任务名',
                btn11:{
                    required:'请选择开始时间',
                    date: '请正确填写日期'
                },
                project1: '请选择项目',
                executor2: '请选择执行人',
                progress: {
                    required: '请正确填写进度',
                    range:'进度范围为 0-100'
                },
                work: {
                    required: '请填写工作量',
                    number:'请正确填写工作量'
                },

            }
        });
    },
};
task.init();
